define(["app",
    "resources/ProcessResource"], function (app) {

    var deps = ["ProcessResource"];

    function service (ProcessResource) {
        var unReadMessageList = [];
        var messageList = [];
        return {
            removeMessage: function (procMessageId) {
                for (var i = 0; i < unReadMessageList.length; i++) {
                    var item = unReadMessageList[i];
                    if (item.procMessageId == procMessageId) {
                        unReadMessageList.splice(i, 1);
                        break;
                    }
                }
                for (var i = 0; i < messageList.length; i++) {
                    var item = messageList[i];
                    if (item.procMessageId == procMessageId) {
                        item.isRead = 1;
                        break;
                    }
                }
            },
            getUnReadMessage: function () {
                return unReadMessageList;
            },

            setUnReadMessage: function (data) {
                //ProcessResource.getUnReadProcMessageList().success(
                //function(data) {
                unReadMessageList.splice(0, unReadMessageList.length);
                for (var i = 0; i < data.length; i++) {
                    data[i].time = getTime(data[i].createtime);
                    unReadMessageList.push(data[i]);
                }
                //});

            },
            getMessage: function () {
                return messageList;
            },
            setMessage: function () {
                ProcessResource.getProcMessageList().success(function (data) {
                    messageList.splice(0, messageList.length - 1);
                    for (var i = 0; i < data.length; i++) {
                        messageList.push(data[i]);
                    }
                });

            }
        };
    }

    function getTime (time) {
        var now = new Date().getTime();
        var d = now - time;
        if (d < 3600000) {
            return "1 小时内";
        }
        if (d >= 3600000 && d < 24 * 3600000) {
            return "1 小时前";
        }
        if (d > 24 * 3600000 && d < 3 * 24 * 3600000) {
            return "1 天前";
        }
        if (d >= 3 * 24 * 3600000 && d < 7 * 24 * 3600000) {
            return "3 天前";
        }
        if (d >= 7 * 24 * 3600000) {
            return "1 周前";
        }
    }

    service.$inject = deps;
    app.lazy.service("ProcessMessageService", service);
});
